package com.etbsms.security;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.UserDetails;

import com.etbsms.domain.Users;
import com.etbsms.util.Constants;

public class User implements UserDetails {

	private static final long serialVersionUID = -8266098568582366501L;
	private String username;
	private String password;
	private Users users;

	public User(String username, String password,Users users) {
		this.username = username;
		this.password = password;
		this.setUsers(users);
		this.getUsers().setCurrent_role(Constants.CURRENT_ROLE_MAP.get(users.getSeq_no()));
		Constants.CURRENT_ROLE_MAP.remove(users.getSeq_no());
		
	}

	@Override
	public Collection<GrantedAuthority> getAuthorities() {
		List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
		grantedAuthorities.add(new GrantedAuthorityImpl(this.users.getCurrent_role().getName()));	
		return grantedAuthorities;
	}	

	@Override
	public String getPassword() {
		return password;
	}

	@Override
	public String getUsername() {
		return username;
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}

	public void setUsers(Users users) {
		this.users = users;
	}

	public Users getUsers() {
		return users;
	}
}
